package light.head.controller.inv;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.inv.InvDepotDAO;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.inv.InvDepot;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping(value = Url.C_INVDEPOT)
public class InvDepotController extends SupporterController{
	private InvDepotDAO deDAO;
	private OrganDAO organDAO;
	
	
	@RequestMapping(value = Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response){
		return new ModelAndView(Url.INVDEPOT_INIT);
	}
	
	@RequestMapping(value = Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response){
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME, Parm.ON, Parm.CN, Parm.TIME1, Parm.TIME2, Parm.DELETED);
		if (deDAO.findByMap(organDAO.getSonCodeList(cu_organCode(request)), map, pb) == RESULT.SUCCESS) {
			request.setAttribute(Parm.PAGE_BEAN, pb);
		}
		return new ModelAndView(Url.INVDEPOT_LIST);
	}
	
	
	@RequestMapping(value = Url.EDIT)
	public ModelAndView edit(HttpServletRequest request, HttpServletResponse response){
		String code = request.getParameter(Parm.CODE);
		if (null != code) {
			InvDepot de = deDAO.get(Integer.parseInt(code));
			request.setAttribute(Parm.INVDEPOT, de);
		}
		return new ModelAndView(Url.INVDEPOT_EDIT);
	}
	
	@RequestMapping(value = Url.SAVE)
	public void save(InvDepot de, HttpServletRequest request, HttpServletResponse response) throws Exception{
		if (de == null) {
			print(response, Msg.PARAMETER_ERROR);
			return ;
		}
		RESULT result = RESULT.ERROR;
		if (de.getCode() == null) {
			Integer oc =organDAO.getRoot(cu_organCode(request)).getCode();
			List<InvDepot> gs = deDAO.findByNameOc(de.getName(), organDAO.getSonCodeList(oc));
			if (gs != null && gs.size() > 0)  {
				print(response, Msg.EXISTS);
				return;
			}
			de.setOc(cu_organCode(request));
			de.setCu(cu_code(request));
			de.setCt(new Date());
			de.setDeleted(false);
			result = deDAO.add(de);
		} else {
			result = deDAO.update(de);
		} if (RESULT.SUCCESS == result) {
			print(response, Msg.SUCCESS);
		} else
			print(response, Msg.ERROR);
	}
	
	@RequestMapping(value = Url.SHIELD)
	public void shield(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		InvDepot de = deDAO.get(Integer.parseInt(code));
		if (null == de) {
			print(response, Msg.NOT_FIND);
			return;
		}
		de.setDeleted(true);
		if (deDAO.upd(de) == RESULT.SUCCESS)
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
	}
	
	@RequestMapping(value = Url.UN_SHIELD)
	public void unshield(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String code = request.getParameter(Parm.CODE);
		if (!isNotNull(code)) {
			print(response, Msg.PARAMETER_ERROR);
			return;
		}
		InvDepot de = deDAO.get(Integer.parseInt(code));
		if (null == de) {
			print(response, Msg.NOT_FIND);
			return;
		}
		de.setDeleted(false);
		if (deDAO.upd(de) == RESULT.SUCCESS)
			print(response, Msg.SUCCESS);
		else
			print(response, Msg.ERROR);
	}
	@Autowired
	public void setDeDAO(InvDepotDAO deDAO) {
		this.deDAO = deDAO;
	}
	@Autowired
	public void setOrganDAO(OrganDAO organDAO) {
		this.organDAO = organDAO;
	}
}
